package com.zy.algorithm.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zy.algorithm.R;
import com.zy.algorithm.controller.IndexController;
import com.zy.algorithm.controller.screen.BaseBallFirstScreenController;
import com.zy.algorithm.curtain.IScreenBean;
import com.zy.utils.ActivityUtils;
import com.zy.zlog.ZLog;

public abstract class BaseAlgorithmBallFragment extends BaseAlgorithmFragment {

    private static final String TAG = "BaseAlgorithmBall";

    @Override
    protected int getLayout() {
        return R.layout.fragment_algorithm_base;
    }

    protected TextView[] dataTipsTVS = new TextView[10];
    protected IndexController indexController;
    protected BaseBallFirstScreenController firstScreenController;
    protected ViewGroup sort_index_tips_container;


    @Override
    protected void initView(View view) {
        super.initView(view);
        initEnteringContainer(view);
        indexController = new IndexController(view, stepOne);
        initUnSortView(view);
        adaptScreenOrientationPortrait();

        firstScreenController = buildScreenController(view, getColorAni(), indexController);
        initLayoutContainer(view);
    }

    @Override
    protected void resetAll() {
        firstScreenController.statuesResetAll();
    }

    private void initLayoutContainer(View view) {
        View sort_step_des = view.findViewById(R.id.sort_step_des);
        sort_index_tips_container = view.findViewById(R.id.sort_index_tips_container);
        //竖屏
        if (ActivityUtils.isScreenOrientationPortrait(getActivity())) {
            sort_step_des.setVisibility(View.VISIBLE);
            sort_index_tips_container.setVisibility(View.VISIBLE);
        } else {//横屏
            sort_step_des.setVisibility(View.GONE);
            sort_index_tips_container.setVisibility(View.GONE);
        }
    }

    protected BaseBallFirstScreenController buildScreenController(View view, boolean colorAni, IndexController indexController) {
        return new BaseBallFirstScreenController(view,
                getColorAni(),
                indexController);
    }

    //横竖屏适应
    private void adaptScreenOrientationPortrait() {
        int dataTextSize;
        if (ActivityUtils.isScreenOrientationPortrait(getActivity())) {
            dataTextSize = (int) getResources().getDimension(R.dimen.dp_30);
        } else {//横屏
            dataTextSize = (int) getResources().getDimension(R.dimen.dp_45);
        }

        for (TextView tv : dataTipsTVS) {
            ViewGroup.LayoutParams layoutParams = tv.getLayoutParams();
            layoutParams.width = dataTextSize;
            layoutParams.height = dataTextSize;
            tv.setLayoutParams(layoutParams);
        }
    }

    private void initUnSortView(View view) {
        TextView index0 = view.findViewById(R.id.un_sort_index_0);
        TextView index1 = view.findViewById(R.id.un_sort_index_1);
        TextView index2 = view.findViewById(R.id.un_sort_index_2);
        TextView index3 = view.findViewById(R.id.un_sort_index_3);
        TextView index4 = view.findViewById(R.id.un_sort_index_4);
        TextView index5 = view.findViewById(R.id.un_sort_index_5);
        TextView index6 = view.findViewById(R.id.un_sort_index_6);
        TextView index7 = view.findViewById(R.id.un_sort_index_7);
        TextView index8 = view.findViewById(R.id.un_sort_index_8);
        TextView index9 = view.findViewById(R.id.un_sort_index_9);


        dataTipsTVS[0] = index0;
        dataTipsTVS[1] = index1;
        dataTipsTVS[2] = index2;
        dataTipsTVS[3] = index3;
        dataTipsTVS[4] = index4;
        dataTipsTVS[5] = index5;
        dataTipsTVS[6] = index6;
        dataTipsTVS[7] = index7;
        dataTipsTVS[8] = index8;
        dataTipsTVS[9] = index9;
    }

    private View entering_container;
    private View algorithm_context_container;

    private void initEnteringContainer(View view) {
        entering_container = view.findViewById(R.id.entering_container);
        algorithm_context_container = view.findViewById(R.id.algorithm_context_container);
    }

    @Override
    protected View getEnterContainer() {
        return entering_container;
    }

    @Override
    protected View getContextContainer() {
        return algorithm_context_container;
    }

    @Override
    protected void setScreenDataTips(IScreenBean iScreenBean) {
        super.setScreenDataTips(iScreenBean);

        for (int index = 0; index < dataTipsTVS.length; index++) {
            TextView textView = dataTipsTVS[index];
            textView.setText(Integer.toString(iScreenBean.getSortScreenStart()[index]));
        }
    }

    @Override
    protected void setScreenData(IScreenBean bean, boolean start) {
        firstScreenController.setScreenData(bean, start);
    }

    @Override
    protected void sortAnimation(int index, IScreenBean screenBean, StepListener listener) {
        ZLog.d(TAG, "" + index + " " + screenBean);
        //执行幕动画
        //幕动画结束->把动画数据交给幕动画-控制器
        //子动画结束 -> 幕 退出动画
        //后执行下一个

        if (screenBean.isNeedScreenAnimation()) {

        } else {

        }
        firstScreenController.startSubCartoon(screenBean.getCartoonStepBeanList(), new StepListener() {

            @Override
            public void nextStep() {
                //子动画结束 -> 幕 退出动画
                listener.nextStep();
            }
        });
    }

    protected int getCenterXByIndex(int index) {
        return firstScreenController.getCenterXByIndex(index);
    }
}
